package ${basePackage}.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import com.wht.pirates.page.Pagination;
import com.wht.pirates.result.Result;

import ${basePackage}.dto.${className}SearchDTO;
import ${basePackage}.model.${className};
import ${basePackage}.service.${className}Service;

/**
 * @Author: smili
 */
@RestController
@RequestMapping("/${lowerClassName}")
public class ${className}Controller {

    @Autowired
    private ${className}Service ${lowerClassName}Service;


    /**
     * 请求参数 Date类型转换
     *
     * @param binder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    /**
     * 添加，表单post提交
     *
     * @param ${lowerClassName}
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public Result<String> save(${className} ${lowerClassName}) {

        boolean flag = ${lowerClassName}Service.add(${lowerClassName});

        if (!flag) {
            Result.valueOfError("添加失败");
        }
        return Result.valueOfSuccess("添加成功");
    }

    /**
     * 删除
     *
     * @param ${pkName}
     * @return
     */
    @RequestMapping(value = "/{${pkName}}", method = RequestMethod.DELETE)
    public Result<String> delete(@PathVariable ${pkType} ${pkName}) {

        if (${pkName} == null) {
            return Result.valueOfError("请选择需要删除的资源");
        }

        boolean flag = ${lowerClassName}Service.delete(${pkName});
        if (!flag) {
            return Result.valueOfError("删除失败");
        }

        return Result.valueOfSuccess("删除成功");
    }
    /**
     * 修改，json格式PUT提交
     *
     * @param ${pkName}
     * @param ${lowerClassName}
     * @return
     */
    @RequestMapping(value = "/{${pkName}}", method = RequestMethod.PUT)
    public Result<String> edit(@PathVariable ${pkType} ${pkName},@RequestBody ${className} ${lowerClassName}) {

        if (${lowerClassName}.get${upperPkName}() == null) {
            return Result.valueOfError("请选择要修改的资源");
        }

        boolean flag = ${lowerClassName}Service.update(${lowerClassName});
        if (!flag) {
            return Result.valueOfError("更新失败");
        }

        return Result.valueOfSuccess();
    }

    /**
     * 根据主键获取数据
     *
     * @param ${pkName}
     * @return
     */
    @RequestMapping(value = "/{${pkName}}", method = RequestMethod.GET)
    public Result<${className}> search(@PathVariable ${pkType} ${pkName}) {

        if (${pkName} == null) {
            return Result.valueOfError("请选择需要查看的资源");
        }

        ${className} ${lowerClassName} = ${lowerClassName}Service.search(${pkName});
        if (${lowerClassName} == null) {
            return Result.valueOfError("查看的角色不存在");
        }

        return Result.valueOfSuccess(${lowerClassName});
    }

    /**
     * 分页查询
     *
     * @param ${lowerClassName}SearchDTO
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public Result<Pagination<${className}>> listPage(${className}SearchDTO ${lowerClassName}SearchDTO) {

        Pagination<${className}> pagination = ${lowerClassName}Service.searchForPage(${lowerClassName}SearchDTO);

        return Result.valueOfSuccess(pagination);
    }

}